﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Profession : MonoBehaviour
{
    private List<GameObject> materials = new List<GameObject>();
    [SerializeField]
    private Transform parent;
    [SerializeField]
    private GameObject materialPrefab;
    [SerializeField]
    private Recipe selectedRecipe;
    [SerializeField]
    private Text description;
    [SerializeField]
    private InputField countTxt;
    [SerializeField]
    private Text title;
    [SerializeField]
    private ItemInfo info;
    private List<int> amounts = new List<int>();
    private int maxAmount;
    private int amount;

    public int MyAmount
    {
        set
        {
            countTxt.text = value.ToString();
            amount=value;
        }
        get
        {
            return amount;
        }
    }

    private void Start()
    {
        inventoryScript.MyInstance.itemCountEvent += new ItemCount(UpdateMaterialCount);
        ShowDescription(selectedRecipe);
    }
    public void ShowDescription(Recipe recipe)
    {
        if (selectedRecipe != null)
        {
            selectedRecipe.Deselect();

        }
        this.selectedRecipe = recipe;
        this.selectedRecipe.Select();
        foreach (GameObject go in materials)
        {
            Destroy(go);
        }
        materials.Clear();
        title.text = selectedRecipe.OutputItem.MyTitle;
        description.text = selectedRecipe.Description;
        info.Initialize(recipe.OutputItem, 2);
        foreach (CraftingMaterial tempMaterial in recipe.MyMaterials)
        {
            GameObject tmp = Instantiate(materialPrefab, parent);
            tmp.GetComponent<ItemInfo>().Initialize(tempMaterial.Item, tempMaterial.Count);
            materials.Add(tmp);
        }
        UpdateMaterialCount(null);
    }
    public void Craft(bool craftAll)
    {
        if (CanCraft()&& !Player.MyInstance.IsAttacking && !Player.MyInstance.IsCasting)
        {
            if (!craftAll)
            {
                StartCoroutine(CraftRoutine(int.Parse(countTxt.text)));
            }
            else
            {
                StartCoroutine(CraftRoutine(maxAmount));
            }
        }
    }
    private void UpdateMaterialCount(Item item)
    {
        //maxAmount = amounts.Min();
        //if (amounts.Count > 0)
        //{
        //    Debug.Log("after sort =" + amounts[0]);
        //}
        foreach (GameObject curMaterial in materials)
        {
            ItemInfo tmp = curMaterial.GetComponent<ItemInfo>();
            tmp.UpdateStackCount();
        }
        if (CanCraft())
        {
            
            maxAmount = amounts.Min();
            Debug.Log("maxAmount ="+maxAmount);
            if (countTxt.text=="0"||countTxt.text==null) 
            {
                MyAmount = 1;
                
            }
            //else if (int.Parse(countTxt.text) > maxAmount)
            //{
            //    MyAmount = maxAmount;
                
            //}
            else
            {
                MyAmount = maxAmount;
            }
        }
        else 
        {
            MyAmount = 0;
        }
    }
    public void AddItemsToInventory()
    {
        if (inventoryScript.MyInstance.AddItem(info.MyItem))
        {
            foreach (CraftingMaterial material in selectedRecipe.MyMaterials)
            {
                for (int i = 0; i < material.Count; i++)
                {
                    inventoryScript.MyInstance.RemoveItem(material.Item);
                }
            }
        }
    }
    private IEnumerator CraftRoutine(int count)
    {
        for (int i = 0; i < count; i++) 
        {
            yield return Player.MyInstance.MyInitRoutine = StartCoroutine(Player.MyInstance.CraftRoutine(selectedRecipe));
        }
    }
    private bool CanCraft()
    {
        amounts = new List<int>();
        foreach (CraftingMaterial material in selectedRecipe.MyMaterials)
        {
            int count = inventoryScript.MyInstance.GetItemCount(material.Item.MyTitle);
            if(count < material.Count)
            {
                return false;
            }
            amounts.Add((int)(count/material.Count));
        }
        return true;
    }
}
